package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.danmo.domain.DanmoInformationCopy1;
import com.muyu.danmo.domain.DanmoInformationPhotos;
import com.muyu.danmo.domain.DanmoUserFollows;
import com.muyu.danmo.mapper.DanmoInformationCopy1Mapper;
import com.muyu.danmo.mapper.DanmoInformationPhotosMapper;
import com.muyu.danmo.mapper.DanmoUserFollowersMapper;
import com.muyu.danmo.mapper.DanmoUserFollowsMapper;
import com.muyu.danmo.service.IDanmoUserFollowsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 关注关系Service业务层处理
 *
 * @author yangle
 * @date 2024-11-15
 */
@Service
public class DanmoUserFollowsServiceImpl implements IDanmoUserFollowsService {
    @Autowired
    private DanmoUserFollowsMapper danmoUserFollowsMapper;

    @Autowired
    private DanmoUserFollowersMapper danmoUserFollowersMapper;

    /**
     * 查询关注关系
     *
     * @param followId 关注关系主键
     * @return 关注关系
     */
    @Override
    public DanmoUserFollows selectDanmoUserFollowsByFollowId(Long followId) {
        return danmoUserFollowsMapper.selectDanmoUserFollowsByFollowId(followId);
    }

    /**
     * 查询关注关系列表
     *
     * @param danmoUserFollows 关注关系
     * @return 关注关系
     */
    @Override
    public List<DanmoUserFollows> selectDanmoUserFollowsList(DanmoUserFollows danmoUserFollows) {
        return danmoUserFollowsMapper.selectDanmoUserFollowsList(danmoUserFollows);
    }

    /**
     * 新增关注关系
     *
     * @param danmoUserFollows 关注关系
     * @return 结果
     */
    @Override
    public Result insertDanmoUserFollows(DanmoUserFollows danmoUserFollows) {
        try {
            // 插入关注记录
            DanmoUserFollows danmoUserFollows1 = danmoUserFollowsMapper.selectDanmoUserFollowsByOtherParty(danmoUserFollows);
            if (danmoUserFollows1 != null){
                return Result.reportedanerror("已关注");
            }
            danmoUserFollowsMapper.save(danmoUserFollows);
//            DanmoUserFollowers danmoUserFollowers = new DanmoUserFollowers();
//            danmoUserFollowers.setFollowerId(danmoUserFollows.getFollowerId());
//            danmoUserFollowers.setFolloweeId(danmoUserFollows.getFolloweeId());
//            danmoUserFollowersMapper.insertDanmoUserFollowers(danmoUserFollowers);
            // 检查被关注者是否已经关注了当前用户
            boolean isFollowedBack = danmoUserFollowsMapper.existsByFollowerIdAndFolloweeId(danmoUserFollows.getFolloweeId(), danmoUserFollows.getFollowerId());

            if (isFollowedBack) {
                // 更新两条记录的状态为“互相关注”
                danmoUserFollowsMapper.updateStatusForMutualFollow(danmoUserFollows.getFollowerId(), danmoUserFollows.getFolloweeId());
            }

            return Result.success(danmoUserFollows, "关注成功");
        } catch (Exception e) {
            return Result.reportedanerror("关注失败" + e.getMessage());
        }
    }

    /**
     * 修改关注关系
     *
     * @param danmoUserFollows 关注关系
     * @return 结果
     */
    @Override
    public Result updateDanmoUserFollows(DanmoUserFollows danmoUserFollows) {
        return danmoUserFollowsMapper.updateDanmoUserFollows(danmoUserFollows);
    }

    /**
     * 批量删除关注关系
     *
     * @param followIds 需要删除的关注关系主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoUserFollowsByFollowIds(Long[] followIds) {
        return danmoUserFollowsMapper.deleteDanmoUserFollowsByFollowIds(followIds);
    }

    /**
     * 删除关注关系信息
     *
     * @param followId 关注关系主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoUserFollowsByFollowId(Long followId) {
        return danmoUserFollowsMapper.deleteDanmoUserFollowsByFollowId(followId);
    }

    @Override
    public Result remove(DanmoUserFollows danmoUserFollows) {

        // 根据followId获取关注记录
        DanmoUserFollows follow = danmoUserFollowsMapper.findById(danmoUserFollows);
        if (follow.getFollowerId() == null) {
            return Result.reportedanerror("关注记录不存在");
        }

        // 删除关注记录
        danmoUserFollowsMapper.deleteById(follow.getFollowId());

        // 检查是否需要更新相互关注的状态
        checkAndUpdateMutualFollowStatus(follow.getFollowerId(), follow.getFolloweeId());

        return Result.success(1, "取消关注成功");
    }

    @Override
    public List<DanmoUserFollows> selectDanmoUserFollowsfanList(DanmoUserFollows danmoUserFollows) {
        List<DanmoUserFollows> danmoUserFollowsList = danmoUserFollowsMapper.selectDanmoUserFollowsfanList(danmoUserFollows);
        for (DanmoUserFollows userFollows : danmoUserFollowsList) {
            DanmoUserFollows danmoUserFollows1 = danmoUserFollowsMapper.selectDanmoUserFollowsByUserIdAndFollowId(userFollows.getFollowerId(), userFollows.getFolloweeId());
            if (danmoUserFollows1 == null){
                userFollows.setStatus(null);
            }
        }
        return danmoUserFollowsList;
    }
@Autowired
private DanmoInformationCopy1Mapper danmoInformationCopy1Mapper;
    @Autowired
    private DanmoInformationPhotosMapper danmoInformationPhotosMapper;
    @Override
    public List<DanmoUserFollows> selectUserList(DanmoUserFollows danmoUserFollows) {
            List<DanmoUserFollows> danmoUserFollowsList = danmoUserFollowsMapper.selectDanmoUserFollowsfanList(danmoUserFollows);
        List<Long> longs = new ArrayList<>();

        for (DanmoUserFollows userFollows : danmoUserFollowsList) {
            longs.add(userFollows.getFolloweeId());
        }
        if (longs.size() == 0){
            return danmoUserFollowsList;
        }
        List<DanmoInformationCopy1> danmoInformationCopy1List = danmoInformationCopy1Mapper.selectList(longs);

        for (DanmoUserFollows userFollows : danmoUserFollowsList) {
            for (DanmoInformationCopy1 danmoInformationCopy1 : danmoInformationCopy1List) {
                if (userFollows.getFolloweeId().equals(danmoInformationCopy1.getId())){
                    userFollows.setName(danmoInformationCopy1.getName());
                }
            }
        }
        List<DanmoInformationPhotos> danmoInformationPhotosList = danmoInformationPhotosMapper.selectPhotos(longs);
            for (DanmoUserFollows userFollows : danmoUserFollowsList) {
                for (DanmoInformationPhotos danmoInformationPhotos : danmoInformationPhotosList) {
                    if (userFollows.getFolloweeId().equals(danmoInformationPhotos.getInformationId())){
                        userFollows.setPhotos(danmoInformationPhotos.getPhotos());
                    }
                }
            }
        return danmoUserFollowsList;
    }

    private void checkAndUpdateMutualFollowStatus(Long followerId, Long followeeId) {
        // 检查被关注者是否还关注着关注者
        boolean isStillFollowed = danmoUserFollowsMapper.existsByFollowerIdAndFolloweeId(followeeId, followerId);

        if (isStillFollowed) {
            // 如果被关注者还关注着关注者，更新状态为“关注”
            danmoUserFollowsMapper.updateStatusToFollow(followeeId, followerId);
        }
    }

}
